<template>
	<!-- 拼团商品规格弹窗 -->
	<su-popup :show="show" round="10" @close="emits('close')">
		<view class="ss-modal-box bg-white ss-flex-col">
			<view class="modal-header ss-flex ss-col-center">
				<view class="header-left ss-m-r-30">
					<image
						class="sku-image"
						:src="$url.cdn(state.selectedSkuPrice.image || goodsInfo.image)"
						mode="aspectFill"
					>
					</image>
				</view>
				<view class="header-right ss-flex-col ss-row-between ss-flex-1">
					<view class="goods-title ss-line-2">
						<view class="tig ss-flex ss-col-center">
							<view class="tig-icon ss-flex ss-col-center ss-row-center">
								<view class="groupon-tag">
									<image
										:src="
											$url.static(
												'/static/img/shop/goods/groupon-tag-white.png',
											)
										"
									>
									</image>
								</view>
							</view>
							<view class="tig-title">拼团价</view>
						</view>
						<view class="info-title">
							{{ goodsInfo.title }}
						</view>
					</view>
					<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
						<view class="price-text"> {{ goodsPrice }}</view>

						<view class="stock-text ss-m-l-20">
							库存{{ state.selectedSkuPrice.stock || goodsInfo.stock }}件
						</view>
					</view>
				</view>
			</view>
			<view class="modal-content ss-flex-1">
				<scroll-view scroll-y="true" class="modal-content-scroll">
					<view
						v-if="grouponAction === 'create' && activityType === 'groupon_ladder'"
						class="sku-item ss-m-b-20"
					>
						<view class="label-text ss-m-b-20">拼团人数</view>
						<view class="ss-flex ss-col-center ss-flex-wrap">
							<button
								v-for="(ladder, key) in goodsInfo.activity.rules.ladders"
								:key="key"
								class="ss-reset-button spec-btn"
								:class="[
									{
										'checked-btn': grouponNum == ladder,
									},
								]"
								@tap="onSelectLadder(ladder)"
							>
								{{ ladder }}人团
							</button>
						</view>
					</view>
					<view class="sku-item ss-m-b-20" v-for="sku1 in goodsInfo.attrs" :key="sku1.id">
						<view class="label-text ss-m-b-20">{{ sku1.title }}</view>
						<view class="ss-flex ss-col-center ss-flex-wrap">
							<button
								class="ss-reset-button spec-btn"
								v-for="sku2 in sku1.children"
								:class="[
									{
										'checked-btn':
											state.currentSkuArray[sku1.title] == sku2.title,
									},
									{
										'disabled-btn': sku2.disabled == true,
									},
								]"
								:key="sku2.title"
								:disabled="sku2.disabled == true"
								@tap="onSelectSku(sku1.title, sku2.title)"
							>
								{{ sku2.title }}
							</button>
						</view>
					</view>
					<view class="buy-num-box ss-flex ss-col-center ss-row-between">
						<view class="label-text">购买数量</view>
						<su-number-box
							:min="1"
							:max="state.selectedSkuPrice.stock"
							:step="1"
							v-model="state.selectedSkuPrice.goodsNum"
							@change="onNumberChange($event)"
							activity="groupon"
						></su-number-box>
					</view>
				</scroll-view>
			</view>
			<view class="modal-footer ss-p-y-20">
				<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center">
					<view class="ss-flex">
						<button class="ss-reset-button origin-price-btn ss-flex-col">
							<view class="btn-title">{{ grouponNum + '人团' }}</view>
						</button>
						<button class="ss-reset-button btn-tox ss-flex-col" @tap="onBuy">
							<view class="btn-price">
								{{
									grouponAction === 'create' &&
									goodsInfo.activity.rules.leaderDiscount == 1
										? leaderPrice
										: goodsPrice
								}}
							</view>
							<view
								v-if="
									grouponAction === 'create' &&
									goodsInfo.activity.rules.leaderDiscount == 0
								"
								>立即开团</view
							>
							<view
								v-else-if="
									grouponAction === 'create' &&
									goodsInfo.activity.rules.leaderDiscount == 1
								"
								>团长立减价</view
							>
							<view v-else-if="grouponAction === 'join'">参与拼团</view>
						</button>
					</view>
				</view>
			</view>
		</view>
	</su-popup>
</template>

<script setup>
	import { computed, reactive, watch } from 'vue';
	import { formatPrice } from '/shop/hooks/useGoods';
	import { isEmpty, _ } from 'lodash';
	import $url from '/shop/url/index.js';
	import $helper from '/shop/helper/index.js';
	const headerBg = $url.css('/static/img/shop/goods/groupon-btn-long.png');
	const emits = defineEmits(['change', 'addCart', 'buy', 'close', 'ladder']);
	const props = defineProps({
		show: {
			type: Boolean,
			default: false,
		},
		goodsInfo: {
			type: Object,
			default() {},
		},
		grouponAction: {
			type: String,
			default: 'create',
		},
		grouponNum: {
			type: [Number, String],
			default: 0,
		},
	});
	const state = reactive({
		selectedSkuPrice: {},
		currentSkuArray: [],
		grouponNum: props.grouponNum,
	});
	//输入框改变数量
	function onNumberChange(e) {
		if (e === 0) return;
		if (state.selectedSkuPrice.goodsNum === e) return;
		state.selectedSkuPrice.goodsNum = e;
	}

	// 默认单规格
	// if (!props.goodsInfo.is_sku) {
	// 	state.selectedSkuPrice = props.goodsInfo.sku_prices[0];
	// }

	if (props.goodsInfo.spec === 1) {
		state.selectedSkuPrice = props.goodsInfo.goodsSkus[0];
		state.selectedSkuPrice.skus.forEach((e) => {
			state.currentSkuArray[e.key] = e.value;
		});

		console.log('state.currentSkuArray: ', state.currentSkuArray);
		state.selectedSkuPrice.goodsNum = 1;
	} else {
		// 构建同一个属性方便处理
		state.selectedSkuPrice = {
			stock: state.goodsInfo.stock,
			goodsNum: 1,
			price: state.goodsInfo.price,
		};
	}

	// 活动类型
	const activityType = props.goodsInfo.activity.type;

	// 可选规格
	const skuPrices = computed(() => {
		// let skuPrices = props.goodsInfo.sku_prices;
		// if (props.goodsInfo.is_sku) {
		// 	skuPrices.forEach((item) => {
		// 		item.goods_sku_id_arr = item.goods_sku_ids.split(',');
		// 	});
		// }
		// return skuPrices;
	});

	// const skuList = props.goodsInfo.skus;

	// 规格价格
	const goodsPrice = computed(() => {
		if (isEmpty(state.selectedSkuPrice)) {
			return formatPrice(props.goodsInfo.groupPrice);
		}

		// 拼团价格
		if (activityType === 'groupon') {
			// return props.goodsInfo.activity.groupOnActivity.groupPrice;
			return state.selectedSkuPrice.groupPrice;
		}
	});

	// 团长优惠
	const leaderPrice = computed(() => {
		if (isEmpty(state.selectedSkuPrice)) {
			return formatPrice(props.goodsInfo.leaderPrice);
		}

		if (activityType === 'groupon') {
			return state.selectedSkuPrice.leaderPrice;
		}
	});

	watch(
		() => state.selectedSkuPrice,
		(newVal) => {
			emits('change', newVal);
		},
		{
			immediate: true, // 立即执行
			deep: true, // 深度监听
		},
	);

	// 点击购买
	function onBuy() {
		if (state.selectedSkuPrice.stock <= 0) {
			$helper.toast('库存不足');
			return;
		}
		emits('buy', state.selectedSkuPrice);
	}

	// 改变禁用状态
	function changeDisabled(isChecked = false, pid = 0, skuId = 0) {}

	// 当前所选规格下，获取所有有库存的 skuPrice
	function getCanUseSkuPrice() {
		let goodSkuPrice = _.find(props.goodsInfo.goodsSkus, (e) => {
			let skus = e.skus;
			return _.every(skus, (kv) => {
				let key = kv.key;
				let value = kv.value;
				let skuv = state.currentSkuArray[key];
				return value == skuv;
			});
		});
		console.log('选中的skus: ', goodSkuPrice);
		return goodSkuPrice;
	}

	// 选择规格
	function onSelectSku(key, value) {
		state.currentSkuArray[key] = value;
		// // 当前所选规格下，所有可以选择的 skuPric
		state.selectedSkuPrice = getCanUseSkuPrice();
		// 改变规格项禁用状态
		// changeDisabled(isChecked, pid, skuId);
	}

	changeDisabled(false);
</script>

<style lang="scss" scoped>
	// 购买
	.buy-btn {
		margin: 0 20rpx;
		width: 100%;
		height: 80rpx;
		border-radius: 40rpx;
		background: linear-gradient(90deg, #ff6000, #fe832a);
		color: #fff;
	}
	.btn-tox {
		width: 382rpx;
		height: 80rpx;
		font-size: 24rpx;
		font-weight: 600;
		margin-left: -50rpx;
		background-image: v-bind(headerBg);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		color: #ffffff;
		line-height: normal;
		border-radius: 0px 40rpx 40rpx 0px;

		.btn-price {
			font-family: OPPOSANS;

			&::before {
				content: '￥';
			}
		}
	}
	.origin-price-btn {
		width: 370rpx;
		height: 80rpx;
		background: rgba(#ff5651, 0.1);
		color: #ff6000;
		border-radius: 40rpx 0px 0px 40rpx;
		line-height: normal;
		font-size: 24rpx;
		font-weight: 500;

		.btn-price {
			font-family: OPPOSANS;

			&::before {
				content: '￥';
			}
		}

		.btn-title {
			font-size: 28rpx;
		}
	}

	.ss-modal-box {
		border-radius: 30rpx 30rpx 0 0;
		max-height: 1000rpx;

		.modal-header {
			position: relative;
			padding: 80rpx 20rpx 40rpx;

			.sku-image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
			}

			.header-right {
				height: 160rpx;
			}

			.close-icon {
				position: absolute;
				top: 10rpx;
				right: 20rpx;
				font-size: 46rpx;
				opacity: 0.2;
			}

			.goods-title {
				font-size: 28rpx;
				font-weight: 500;
				line-height: 42rpx;
				position: relative;
				.tig {
					border: 2rpx solid #ff6000;
					border-radius: 4rpx;
					width: 126rpx;
					height: 38rpx;
					position: absolute;
					left: 0;
					top: 0;

					.tig-icon {
						width: 40rpx;
						height: 40rpx;
						background: #ff6000;
						margin-left: -2rpx;
						border-radius: 4rpx 0 0 4rpx;

						.groupon-tag {
							width: 32rpx;
							height: 32rpx;
						}
					}

					.tig-title {
						font-size: 24rpx;
						font-weight: 500;
						line-height: normal;
						color: #ff6000;
						width: 86rpx;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}
				.info-title {
					text-indent: 132rpx;
				}
			}

			.price-text {
				font-size: 30rpx;
				font-weight: 500;
				color: $red;
				font-family: OPPOSANS;

				&::before {
					content: '￥';
					font-size: 24rpx;
				}
			}

			.stock-text {
				font-size: 26rpx;
				color: #999999;
			}
		}

		.modal-content {
			padding: 0 20rpx;

			.modal-content-scroll {
				max-height: 600rpx;

				.label-text {
					font-size: 26rpx;
					font-weight: 500;
				}

				.buy-num-box {
					height: 100rpx;
				}

				.spec-btn {
					height: 60rpx;
					min-width: 100rpx;
					padding: 0 30rpx;
					background: #f4f4f4;
					border-radius: 30rpx;
					color: #434343;
					font-size: 26rpx;
					margin-right: 10rpx;
					margin-bottom: 10rpx;
				}

				.checked-btn {
					background: linear-gradient(90deg, #ff6000, #fe832a);
					font-weight: 500;
					color: #ffffff;
				}

				.disabled-btn {
					font-weight: 400;
					color: #c6c6c6;
					background: #f8f8f8;
				}
			}
		}
	}

	image {
		width: 100%;
		height: 100%;
	}
</style>
